/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.jb.coinche.client.ui.card.impl;

import org.jb.coinche.client.business.model.Card;
import org.jb.coinche.client.ui.card.CardPanel;
import org.jb.coinche.client.ui.rsc.CardImageResource;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.AbsoluteData;
import com.extjs.gxt.ui.client.widget.layout.AbsoluteLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.Image;

public class CardPanelImpl extends LayoutContainer implements CardPanel {

	public CardPanelImpl(boolean bottom) {
		super( new AbsoluteLayout() );
		this.bottom = bottom;
		setSize( 104, 164 );
		
		setParameters( "", "" );
	}
	
	/*
	 * 
	 */
	private AbsoluteData getTextPosition() {
		if (!bottom) {
			return new AbsoluteData(0, 0);
		} else {
			return new AbsoluteData(0, 154);
		}
	}
	
	/*
	 * 
	 */
	private AbsoluteData getImagePosition() {
		if (!bottom) {
			return new AbsoluteData(0, 14);
		} else {
			return new AbsoluteData(0, 0);
		}
	}
	
	/*
	 * 
	 */
	private AbsoluteData getStatusPosition() {
		if (!bottom) {
			return new AbsoluteData(0, 74);
		} else {
			return new AbsoluteData(0, 60);
		}
	}
	
	/*
	 * 
	 */
	private AbsoluteData getWaitPosition() {
		if (!bottom) {
			return new AbsoluteData(48, 74);
		} else {
			return new AbsoluteData(48, 60);
		}
	}
	
	/*
	 * 
	 */
	private AbsoluteData getActionPosition() {
		if (!bottom) {
			return new AbsoluteData(10, 74);
		} else {
			return new AbsoluteData(10, 60);
		}
	}
	
	/* (non-Javadoc)
	 * @see org.jb.coinche.client.gui.card.impl.CardPanel#setParameters(java.lang.String, org.jb.coinche.client.business.model.Card)
	 */
	public void setParameters( String player, Card card ) {
		
		removeAll();

		Text playerText = new Text( player );
		playerText.setWidth(104);
		playerText.setStyleAttribute("text-align", "center");
		add( playerText, getTextPosition() );
			
		Image image = new Image(getImage(card));
		add( image, getImagePosition() );	
				
		layout();
	}
	
	/*
	 * 
	 */
	/* (non-Javadoc)
	 * @see org.jb.coinche.client.gui.card.impl.CardPanel#setParameters(java.lang.String, java.lang.String)
	 */
	public void setParameters( String player, String text ) {
		
		removeAll();
		
		Text playerText = new Text( player );
		playerText.setWidth(104);
		playerText.setStyleAttribute("text-align", "center");
		add( playerText, getTextPosition() );
			
		Html html = new Html();
		html.setHtml( "<div style=\"background: #F0F0F0; width: 104; height:154\"></div>" );
		add( html, getImagePosition() );
			
		Text paramText = new Text( text );
		paramText.setStyleAttribute("text-align", "center");
		paramText.setWidth(104);
		add( paramText, getStatusPosition() );
		
		layout();
		
	}
	
	@Override
	public void setParameters(String player, String action, final ActionHandler handler) {
		
		removeAll();
		
		Text playerText = new Text( player );
		playerText.setWidth(104);
		playerText.setStyleAttribute("text-align", "center");
		add( playerText, getTextPosition() );
		
		Html html = new Html();
		html.setHtml( "<div style=\"background: #F0F0F0; width: 104; height:154\"></div>" );
		add( html, getImagePosition() );
		
		Button btn = new Button( action );
		btn.addSelectionListener( new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				handler.execute();
			}
		});
		btn.setWidth( 80 );
		add( btn, getActionPosition() );
		
		layout();
		
	}
	
	/*
	 * 
	 */
	/* (non-Javadoc)
	 * @see org.jb.coinche.client.gui.card.impl.CardPanel#setWaiting(java.lang.String)
	 */
	public void setWaiting( String player ) {
		
		removeAll();
		
		Text playerText = new Text( player );
		playerText.setWidth(104);
		playerText.setStyleAttribute("text-align", "center");
		add( playerText, getTextPosition() );
			
		Html html = new Html();
		html.setHtml( "<div style=\"background: #FBF0E4; border: solid 1px; width: 104; height:154\"></div>" );
		add( html, getImagePosition() );
		
		Image imageWait = new Image( GWT.getHostPageBaseURL() + "img/wait.gif");
		add( imageWait, getWaitPosition() );
					
		layout();
	}
	
	/*
	 * 
	 */
	public static ImageResource getImage( Card card ) {
		switch( card.getSuit() ) {
		case CLUBS:
			switch( card.getValue() ) {
			case AS:	return rsc.card1C();
			case SEVEN:	return rsc.card7C();
			case EIGHT:	return rsc.card8C();
			case NINE:	return rsc.card9C();
			case TEN:	return rsc.cardTC();
			case JACK:	return rsc.cardJC();
			case QUEEN:	return rsc.cardQC();
			case KING:	return rsc.cardKC();
			}
		case DIAMONDS:
			switch( card.getValue() ) {
			case AS:	return rsc.card1D();
			case SEVEN:	return rsc.card7D();
			case EIGHT:	return rsc.card8D();
			case NINE:	return rsc.card9D();
			case TEN:	return rsc.cardTD();
			case JACK:	return rsc.cardJD();
			case QUEEN:	return rsc.cardQD();
			case KING:	return rsc.cardKD();
			}		
		case HEARTS:
			switch( card.getValue() ) {
			case AS:	return rsc.card1H();
			case SEVEN:	return rsc.card7H();
			case EIGHT:	return rsc.card8H();
			case NINE:	return rsc.card9H();
			case TEN:	return rsc.cardTH();
			case JACK:	return rsc.cardJH();
			case QUEEN:	return rsc.cardQH();
			case KING:	return rsc.cardKH();
			}		
		case SPADES:
			switch( card.getValue() ) {
			case AS:	return rsc.card1S();
			case SEVEN:	return rsc.card7S();
			case EIGHT:	return rsc.card8S();
			case NINE:	return rsc.card9S();
			case TEN:	return rsc.cardTS();
			case JACK:	return rsc.cardJS();
			case QUEEN:	return rsc.cardQS();
			case KING:	return rsc.cardKS();
			}		
		}
		return null;
	}
	
	private boolean bottom;
	private final static CardImageResource rsc = GWT.create(CardImageResource.class);
	
}
